package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.discovery.bean.SerialChapterListResponse;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class SerialChapterListAdapter extends RecyclerView.Adapter<SerialChapterListAdapter.ViewHolder> {

    private Context context;
    private List<SerialChapterListResponse.ResponseBean> list;
    private AdapterListener listener;

    public void setAdapterListener(AdapterListener listener) {
        this.listener = listener;
    }

    public SerialChapterListAdapter(Context context, List<SerialChapterListResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_serial_chapter_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.chapterNum.setText(list.get(i).getSort() < 10 ?
                "0" + list.get(i).getSort() : "" + list.get(i).getSort());
        viewHolder.chapterName.setText(list.get(i).getTitle());
        viewHolder.chapterDate.setText(DateUtils.ConvertDate(list.get(i).getCheck_datetime()));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SingleArticleDetailsActivity.class);
                intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, convertBean(list.get(i)));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView chapterNum, chapterName, chapterDate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            chapterNum = itemView.findViewById(R.id.chapter_num);
            chapterName = itemView.findViewById(R.id.chapter_name);
            chapterDate = itemView.findViewById(R.id.chapter_date);
        }
    }

    private ArticleDetaisRequest convertBean(SerialChapterListResponse.ResponseBean response) {
        ArticleDetaisRequest bean = new ArticleDetaisRequest();
        bean.setContribute_id(response.getContribute_id());
        bean.setType(Constant.FROM_RECOMMEND);
        bean.setTopic_id(0);
        return bean;
    }
}
